package com.nineclock.system.controller;

import com.nineclock.attendance.dto.DepartmentOrUserSimpleDTO;
import com.nineclock.api.system.OrganizationControllerApi;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.system.service.SysDepartmentService;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.dto.SysDepartmentDTO;
import com.nineclock.system.service.SysCompanyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
@RequestMapping("/organization")
public class OrganizationController implements OrganizationControllerApi {

    @Autowired
    SysCompanyUserService companyUserService;

    @Autowired
    SysDepartmentService departmentService;

//查询组织所有员工
    @Override
    @GetMapping("/members/simple")
    public Result<List<SysCompanyUserDTO>> queryCompanyMemberList() {
        List<SysCompanyUserDTO> companyUserDTOList = companyUserService.queryCompanyMemberList();
        return Result.success(companyUserDTOList);
    }
//部门查询
@Override
@GetMapping("/department")
public Result<List<SysDepartmentDTO>> queryDepartmentList() {
    List<SysDepartmentDTO> departmentDTOList = departmentService.queryDepartmentList();
    return Result.success(departmentDTOList);
}

    //员工查询
    @Override
    @GetMapping("/members")
    public Result<PageResult<SysCompanyUserDTO>> members(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            String keyword,
            Long departmentId) {
        PageResult<SysCompanyUserDTO> pageResult = companyUserService.members(page, pageSize, keyword, departmentId);

        return Result.success(pageResult);
    }
    @Override
    @PostMapping("/uploadExcel")
    public Result uploadExcel(MultipartFile excelFile) throws IOException {
        companyUserService.importData(excelFile);
        return Result.success();
    }



    /**
     * //查询企业ID下的所有顶级部门ID
     * @return
     */
    @GetMapping("/department/top")
    public Result<List<Long>> queryDepartmentIds(){
        List<Long> depIds = departmentService.queryDepartmentIds();
        return Result.success(depIds);
    }

    //根据部门ID, 查询该部门下的员工数量
    @GetMapping("/department/membernum")
    public Result<Integer> queryUserCountByDepartmentIds(@RequestParam("departmentIds") List<Long> departmentIds){
        Integer count = companyUserService.queryUserCountByDepartmentIds(departmentIds);
        return Result.success(count);
    }
//根据部门ID查询子部门及员工列表
@Override
@GetMapping("/simple")
public Result<List<DepartmentOrUserSimpleDTO>> querySimpleDepartment(
        @RequestParam(value = "id", defaultValue = "0") Long departmentId,
        Integer includeMember) {
    List<DepartmentOrUserSimpleDTO> departmentOrUserSimpleDTOList = companyUserService.querySimpleDepartment(departmentId ,includeMember);
    return Result.success(departmentOrUserSimpleDTOList);
}


}
